package com.vegetable.modules.controller.dashboard;

import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.dashboard.DashboardOverviewVO;
import com.vegetable.modules.service.dashboard.IDashboardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 仪表板控制器
 *
 * @author qoder
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/dashboard")
@AllArgsConstructor
@Api(tags = "仪表板管理")
public class DashboardController extends AbstractController {

    private final IDashboardService dashboardService;

    @SysLog(value = "查看仪表板总览")
    @GetMapping("/overview")
    @ApiOperation(value = "获取仪表板总览数据", notes = "获取首页仪表板的所有统计数据")
    public R<DashboardOverviewVO> getDashboardOverview(
            @ApiParam(value = "统计时间范围(天数)", example = "30") 
            @RequestParam(value = "days", defaultValue = "30") Integer days) {
        
        log.info("用户{}请求仪表板总览数据，时间范围：{}天", getUserId(), days);
        
        try {
            DashboardOverviewVO overview = dashboardService.getDashboardOverview(days);
            return R.data(overview);
        } catch (Exception e) {
            log.error("获取仪表板总览数据失败", e);
            return R.fail("获取仪表板数据失败：" + e.getMessage());
        }
    }

    @SysLog(value = "查看仪表板概要")
    @GetMapping("/summary")
    @ApiOperation(value = "获取仪表板概要数据", notes = "获取仪表板的核心统计指标")
    public R<DashboardOverviewVO> getDashboardSummary() {
        
        log.info("用户{}请求仪表板概要数据", getUserId());
        
        try {
            // 获取7天的数据作为概要
            DashboardOverviewVO overview = dashboardService.getDashboardOverview(7);
            return R.data(overview);
        } catch (Exception e) {
            log.error("获取仪表板概要数据失败", e);
            return R.fail("获取仪表板数据失败：" + e.getMessage());
        }
    }

}